package icu.lookyousmileface.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author starrysky
 * @title: SecretaryConfiguration
 * @projectName spring_secretary
 * @description: config layer
 * @date 2021/2/1014:14
 */
@EnableWebSecurity
public class SecretaryConfiguration extends WebSecurityConfigurerAdapter {
//    授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

//        没有权限就跳到登陆页面
        http.formLogin().usernameParameter("users").passwordParameter("pwds").loginPage("/toLogin").loginProcessingUrl("/login");
//        关闭跨站请求的防御 放行get 请求 注销
        http.csrf().disable();
//        开启注销功能,并且注销成功之后返回主页面
        http.logout().logoutSuccessUrl("/");
//        开启记住我
        http.rememberMe().rememberMeParameter("remember");
    }
//    认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        内存中虚拟数据库  .passwordEncoder设置密码编码格式，以及认证权限
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("ku1").password(new BCryptPasswordEncoder().encode("1234")).roles("vip1")
                .and()
                .withUser("ku2").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1","vip2")
                .and()
                .withUser("ku3").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
    }
}
